import 'package:flutter/material.dart';
import 'lc_tableview_component.dart';
import 'lc_typedef.dart';

/// 用来组合sessionHeader和cell
class LCTableViewSessionItem extends StatefulWidget {
  final IndexedWidgetBuilder viewForHeaderInSection;
  final DidClickedSessionAtIndex didClickedSessionAtIndex;

  final int sessionIndex;
  final double sessionHeaderHeight;
  final IndexPathWidgetBuilder cellForRowAtIndexPath;

  /// 点击了哪个cell
  final NumberOfRowsInSection numberOfRowsInSection;
  final DidClickedCellAtIndexPath didClickedCellAtIndexPath;

  LCTableViewSessionItem({
    /// session
    this.sessionIndex,
    this.sessionHeaderHeight,
    this.viewForHeaderInSection,
    this.didClickedSessionAtIndex,

    /// cell
    this.numberOfRowsInSection,
    this.cellForRowAtIndexPath,
    this.didClickedCellAtIndexPath,
  });

  @override
  _LCTableViewSessionItemState createState() => _LCTableViewSessionItemState();
}

class _LCTableViewSessionItemState extends State<LCTableViewSessionItem> {
  bool isEnabled = true;
  bool isShow = true;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Positioned(
          child: LCTableViewSessionHeader(
            sessionIndex: widget.sessionIndex,
            sessionHeaderHeight: widget.sessionHeaderHeight,
            viewForHeaderInSection: widget.viewForHeaderInSection,
            didClickedSessionAtIndex: (index, child) {
              if (widget.didClickedSessionAtIndex != null) {
                isEnabled = widget.didClickedSessionAtIndex(index, child);
                if (isEnabled) {
                  isShow = !isShow;
                  setState(() {});
                }
              }
              return isEnabled;
            },
          ),
        ),
        Container(
          decoration: BoxDecoration(borderRadius: BorderRadius.circular(5.0)),
          margin: EdgeInsets.only(top: widget.sessionHeaderHeight),
          child: tableViewCell(),
        )
      ],
    );
  }

  Widget tableViewCell() {
    if (isEnabled) {
      return isShow
          ? LCTableViewCellItem(
              sessionIndex: widget.sessionIndex,
              numberOfRowsInSection: widget.numberOfRowsInSection,
              cellForRowAtIndexPath: widget.cellForRowAtIndexPath,
              didClickedCellAtIndexPath: widget.didClickedCellAtIndexPath,
            )
          : Container();
    } else {
      return LCTableViewCellItem(
        sessionIndex: widget.sessionIndex,
        numberOfRowsInSection: widget.numberOfRowsInSection,
        cellForRowAtIndexPath: widget.cellForRowAtIndexPath,
        didClickedCellAtIndexPath: widget.didClickedCellAtIndexPath,
      );
    }
  }
}
